<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>08-贴图2</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    html,
    body {
      width: 100%;
      height: 100%;
      overflow: hidden;
    }
  </style>
</head>

<body>
  <script type="module">
    // 引入three.js
      import * as THREE from "three";
      import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";

      //创建场景
      const scene = new THREE.Scene();

      //创建相机
      const camera = new THREE.PerspectiveCamera(
        45,
        window.innerWidth / window.innerHeight,
        0.1,
        1000
      );
      //设置相机位置
      camera.position.set(0, 40, 20); //统一设置
      //设置相机朝向点位
      camera.lookAt(0, 0, 0);

      //创建渲染器
      const renderer = new THREE.WebGLRenderer({
        antialias: true, //减少边缘齿轮
      });
      renderer.setSize(window.innerWidth, window.innerHeight);
      document.body.appendChild(renderer.domElement);

      //光源
      const ambientLight = new THREE.AmbientLight( 0x404040 ); // 环境光
      const directionalLight = new THREE.DirectionalLight( 0xffffff, 0.5 );//平行光
      directionalLight.position.set(3,3,3) //光源位置
      scene.add(ambientLight,directionalLight)

      //创建结构-几何体
      const geometry = new THREE.PlaneGeometry( 10, 10 );
      const loader = new THREE.TextureLoader() //是异步的
      const texture = loader.load('../images/2.png')
      //创建材质(外观)
      const meterial = new THREE.MeshStandardMaterial({
        // aoMap:texture, //阴影效果贴图
        // normalMap:texture, //凹凸效果贴图
        // roughnessMap:texture, //粗糙度贴图
        displacementMap:texture, //多效果贴图
      })
      //结构和材质组合，生成一个物体
      const mesh = new THREE.Mesh(geometry,meterial);
      mesh.rotation.x = -Math.PI / 2
      scene.add(mesh); //在场景中添加物体

      //控制插件
      const controls = new OrbitControls(camera, renderer.domElement);
      controls.enableDamping = true //开启惯性,需要和update方法配合使用
      controls.addEventListener("change", () => {
        //渲染
        renderer.render(scene, camera);
      });


      //动画函数
      function animate() {
        requestAnimationFrame(animate);
        controls.update()
        renderer.render(scene, camera);
      }
      animate();
    </script>
</body>

</html>